package idv.long.game.physic
{
	import flash.display.DisplayObjectContainer;
	
	import idv.long.util.interfaces.IPhysicsInterface;
	
	import org.cove.ape.APEngine;
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.Group;
	import org.cove.ape.Vector2D;

	public class LPhysicEngine implements IPhysicsInterface
	{		
		private var m_bStart:Boolean = false;
		
		private var m_dyamainc_group:Group;
		private var m_static_group:Group;
		
		public function LPhysicEngine()
		{
		}
		
		public function init(container:DisplayObjectContainer):void
		{
			APEngine.init(0.25);
			APEngine.container = container;

			APEngine.addMasslessForce(new Vector2D(0, 2));
			APEngine.damping = 0.99;
			APEngine.constraintCollisionCycles = 10;
			
			m_dyamainc_group = new Group();
			m_dyamainc_group.collideInternal = true;
			
			m_static_group = new Group();
			m_static_group.collideInternal = true;			
			
			m_dyamainc_group.addCollidableList([m_static_group]);
			
			APEngine.addGroup(m_dyamainc_group);
			APEngine.addGroup(m_static_group);
		}
		
		public function release():void
		{
			APEngine.removeGroup(m_dyamainc_group);
			APEngine.removeGroup(m_static_group);
			
			m_dyamainc_group = null;
			m_static_group = null;
		}
		
		public function update():void
		{
			if(m_bStart)
			{
				APEngine.step();
				
				if(m_static_group.collisionList.length > 0)
				{
					trace("collision!");
				}
			}
		}
		
		public function render():void
		{
			if(m_bStart)
				APEngine.paint();
		}
		
		public function start():void
		{
			m_bStart = true;
		}
		
		public function resume():void
		{
			m_bStart = false;
		}
		
		public function pause():void
		{
			m_bStart = true;
		}
		
		public function addEntity(entity:Object, groupID:int):void
		{
			if(groupID == 0)
				this.m_static_group.addParticle(entity as AbstractParticle);
			else
				this.m_dyamainc_group.addParticle(entity as AbstractParticle);
		}
		
		public function removeEntity(entity:Object):void
		{
			
		}
	}
}